/*
 * Copyright (C) 2024 HiHope Open Source Organization.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import oneConnectShare from '@ohos.oneConnect.fileShare';
import Log from '../../../../../../../../common/src/main/ets/default/Log';
import createOrGet from '../../../../../../../../common/src/main/ets/default/SingleInstanceHelper';

const TAG = 'OneConnectShareModel';
const BUNDLE_NAME = 'com.ohos.systemui';

export interface OneConnectShareSwitchStatusListener {
  updateOneConnectShareSwitchStatus(status: boolean): void;
}

export class OneConnectShareService {
  mIsStart = false;
  mListener: OneConnectShareSwitchStatusListener;
  private shareManager: oneConnectShare.ShareManager;

  constructor() {
  }

  registerListener(listener: OneConnectShareSwitchStatusListener): void {
    Log.showInfo(TAG, `registerListener, listener: ${listener}`);
    this.mListener = listener;
  }

  startService(): void {
    if (this.mIsStart) {
      return;
    }
    Log.showInfo(TAG, 'startService');
    this.mIsStart = true;
    this.asyncStartService().then(() => {
    }).catch((err) => {
    });
  }

  async asyncStartService(): Promise<void> {
    Log.showInfo(TAG, `asyncStartService, oneConnectShare module is ${oneConnectShare}`);
    this.shareManager = oneConnectShare?.createShareManager(BUNDLE_NAME);
    Log.showInfo(TAG, `createShareManager result is ${this.shareManager}`);
    this.updateOneConnectShareSwitchStatus();
    this.shareManager?.on('shareStatusChange', (status: boolean) => {
      this.mListener?.updateOneConnectShareSwitchStatus(status);
      Log.showDebug(TAG, `on ShareStatusChange, status: ${JSON.stringify(status)}`);
    })
  }

  stopService(): void {
    if (!this.mIsStart) {
      return;
    }
    Log.showInfo(TAG, 'stopService');
    this.mIsStart = false;
    this.asyncStopService().then(() => {
    }).catch((err) => {
    });
  }

  async asyncStopService(): Promise<void> {
    Log.showInfo(TAG, 'asyncStopService');
    this.shareManager?.off('shareStatusChange');
    oneConnectShare?.releaseShareManager(this.shareManager);
    this.shareManager = undefined;
  }

  updateOneConnectShareSwitchStatus() {
    let status = this.shareManager?.getShareStatus() || false;
    this.mListener?.updateOneConnectShareSwitchStatus(status);
    Log.showDebug(TAG, `getShareStatus, status: ${JSON.stringify(status)}`);
  }

  async changeSwitch(status: boolean): Promise<void> {
    Log.showDebug(TAG, `changeSwitch: ${ status }`);
    if (status) {
      this.shareManager?.enableShare();
    } else {
      this.shareManager?.disableShare();
    }
    this.updateOneConnectShareSwitchStatus();
  }
}


let sOneConnectShareService = createOrGet(OneConnectShareService, TAG);

export default sOneConnectShareService;
